package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.contants.RedisConstants;
import com.heima.common.dtos.PageResponseResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.utils.common.JsonUtils;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Date;



import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import java.util.Date;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import java.util.List;
import java.util.stream.Collectors;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 查询所有违规词并存储在Redis
     * @return
     */
    @Override
    public ResponseResult<List<String>> findAllAndRedis() {

        List<WmSensitive> list = list();
        List<String> collect = list.stream().map(WmSensitive::getSensitives).collect(Collectors.toList());

        redisTemplate.opsForValue().set(RedisConstants.SENSITIVES, JsonUtils.toString(collect));

        return ResponseResult.okResult(collect);
    }





    @Override
    public ResponseResult findSensitive(WmSensitiveDto dto) {

        Integer page = dto.getPage();
        Integer size = dto.getSize();
        String name = dto.getName();

        //创建分页对象
        IPage<WmSensitive> ipage = new Page<>(page,size);
        //创建查询条件
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();

        //敏感词部位空则按照敏感词查找
        if (StringUtils.isNotEmpty(name)){
            wrapper.like(WmSensitive::getSensitives,name);
        }

        ipage = page(ipage, wrapper);



        PageResponseResult pageResponseResult = new PageResponseResult( page,size, (int)ipage.getTotal());
        pageResponseResult.setData(ipage.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        if (StringUtils.isNotEmpty(wmSensitive.getSensitives())){
            WmSensitive sensitive = new WmSensitive();
            sensitive.setSensitives(wmSensitive.getSensitives());
            sensitive.setCreatedTime(new Date());
            save(sensitive);
        }

        //清除Redis缓存
        redisTemplate.delete(RedisConstants.SENSITIVES);
        return ResponseResult.okResult(null);
    }

    //删除违规词
    @Override
    public ResponseResult deleteSensitiveById(Long id) {

        boolean removeById = removeById(id);

        //清除Redis缓存
        redisTemplate.delete(RedisConstants.SENSITIVES);
        return ResponseResult.okResult(null);
    }

    //修改违规词
    @Override
    public ResponseResult updateSensitiveById(WmSensitive sensitive) {

        boolean updateById = updateById(sensitive);

        //清除Redis缓存
        redisTemplate.delete(RedisConstants.SENSITIVES);

        return ResponseResult.okResult(null);
    }


}

